package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.CollectorsUtils;
import com.jrx.anytxn.investor.entity.AmInterestAccrualInvestorInfo;
import com.jrx.anytxn.investor.entity.AmLoanAccountInvestorInfo;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmInterestAccrualInfo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-parent
 * @description: 联合贷批量-更新未抛累计利息
 * @author: xiaoyu.jing
 * @date: 2020-09-01 18:10
 **/
@Order(160)
@Service
public class InvestorUpdateUncastIntrHandle extends BaseBatchAccoutHandle<AmLoanAccountInvestorInfo> {

    Logger logger = LoggerFactory.getLogger(InvestorUpdateUncastIntrHandle.class);

    /**
     * 更新未抛累计累计业务逻辑处理
     * @return
     * @throws TxnException
     */
    @Override
    public List<AmLoanAccountInvestorInfo> handBatchAccout() throws TxnException {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        List<AmLoanAccountInvestorInfo> dealLoanAccountList = custBatchSaveBean.getDealInvestorLoanAccountList();
        if(CollectionUtils.isEmpty(dealLoanAccountList)){
            return null;
        }

        List<AmLoanAccountInvestorInfo> updateLoanList = new ArrayList<>();
        List<AmInterestAccrualInvestorInfo> dealAccuAcctList = custBatchSaveBean.getDealInvestorAccuAcctList();
        if(CollectionUtils.isEmpty(dealAccuAcctList)){
            return null;
        }
        for(AmLoanAccountInvestorInfo loan : dealLoanAccountList) {
            String investorLoanId = loan.getInvestorLoanId();
            BigDecimal intrAmt = loan.getAccruedInterest();
            BigDecimal pentAmt = loan.getAccruedPenalty();
            BigDecimal compAmt = loan.getAccruedCompound();

            List<AmInterestAccrualInvestorInfo> accuAcctList = dealAccuAcctList.stream()
                    .filter(acct -> investorLoanId.equals(acct.getInvestorLoanId()))
                    .collect(Collectors.toList());
            if(CollectionUtils.isEmpty(accuAcctList)){
                continue;
            }

            Map<String, BigDecimal> accuAcctMap = accuAcctList.stream().filter(accu -> TransBizConstant.INTEREST_ACCRUAL_STATUS_1.equals(accu.getAccountStatus()))
                    .collect(Collectors.groupingBy(AmInterestAccrualInvestorInfo::getAccountType,
                            CollectorsUtils.summingBigDecimal(AmInterestAccrualInvestorInfo::getAccrualTotalAccruInterest)));
            //利息
            BigDecimal interestAmount = Optional.ofNullable(accuAcctMap.get(TransBizConstant.INTEREST_ACCRUAL_TYPE_66)).orElse(BigDecimal.ZERO);
            //罚息
            BigDecimal penaltyAmount = Optional.ofNullable(accuAcctMap.get(TransBizConstant.INTEREST_ACCRUAL_TYPE_10)).orElse(BigDecimal.ZERO);
            //复利
            BigDecimal compountAmount = Optional.ofNullable(accuAcctMap.get(TransBizConstant.INTEREST_ACCRUAL_TYPE_20)).orElse(BigDecimal.ZERO);

            AmLoanAccountInvestorInfo updateLoan = new AmLoanAccountInvestorInfo();
            updateLoan.setId(loan.getId());
            updateLoan.setCustomerId(loan.getCustomerId());
            updateLoan.setTenantId(loan.getTenantId());
            boolean isUpdate = false;
            if (intrAmt.compareTo(interestAmount) != 0) {
                isUpdate = true;
                updateLoan.setAccruedInterest(interestAmount);
            }
            if (pentAmt.compareTo(penaltyAmount) != 0) {
                isUpdate = true;
                updateLoan.setAccruedPenalty(penaltyAmount);
            }
            if (compAmt.compareTo(compountAmount) != 0) {
                isUpdate = true;
                updateLoan.setAccruedCompound(compountAmount);
            }

            if (isUpdate) {
                updateLoanList.add(updateLoan);
            }
        }
        return updateLoanList;
    }
}
